package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {
    @Reference
    private InvoiceService invoiceService;

    @Reference
    private ShippingService shippingService;

    @Reference
    private ContractService contractService;

    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/list", name = "展示发票列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int size) {

        //构造查询条件
        InvoiceExample invoiceExample = new InvoiceExample();
        InvoiceExample.Criteria invoiceCriteria = invoiceExample.createCriteria();
        invoiceCriteria.andCompanyIdEqualTo(getLoginCompanyId());
        //分页
        PageInfo info = invoiceService.findAll(page, size, invoiceExample);

        request.setAttribute("page", info);

        return "cargo/invoice/invoice-list";
    }

    /**
     * 跳转至新增发票页面
     * 返回页面包括委托单列表
     *
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        //调用ShippingService查所有委托单
        ShippingExample shippingExample = new ShippingExample();
        ShippingExample.Criteria shippingCriteria = shippingExample.createCriteria();
        shippingCriteria.andCompanyIdEqualTo(getLoginCompanyId());
        shippingCriteria.andStateEqualTo(0);
        List<Shipping> shippingList = shippingService.findAll(shippingExample);

        request.setAttribute("shippings", shippingList);
        return "cargo/invoice/invoice-add";
    }

    /**
     * 增：新增发票
     * 页面上传：
     * 1. 发票的提单编号、邮政信箱（用户输入）
     * 2. 选中的委托对象Shipping
     *
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Invoice invoice, Shipping shipping) {

        invoice.setCompanyId(getLoginCompanyId());
        invoice.setCompanyName(getLoginCompanyName());

        //调用InvoiceService保存
        invoiceService.save(invoice, shipping);

        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        invoiceService.delete(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping("/invoiceExcel")
    public void invoiceExcel(String id) throws IOException {
        Invoice invoice = invoiceService.findById(id);
        Shipping shipping = shippingService.findById(id);
        Packing packing = packingService.findById(id);
        String path = session.getServletContext().getRealPath("/")+"/make/invoicePrint.xlsx";
        Workbook workbook = new XSSFWorkbook(new FileInputStream(path));
        Sheet sheet = workbook.getSheetAt(0);

        sheet.getRow(3).getCell(0).setCellValue(invoice.getSeller());
        sheet.getRow(6).getCell(0).setCellValue(invoice.getBuyer());
        sheet.getRow(6).getCell(8).setCellValue(invoice.getPobox());
        sheet.getRow(11).getCell(2).setCellValue(invoice.getCreateTime());
        sheet.getRow(11).getCell(0).setCellValue(invoice.getId());
        sheet.getRow(11).getCell(5).setCellValue(invoice.getScNo());
        sheet.getRow(11).getCell(7).setCellValue(invoice.getBlNo());
        sheet.getRow(13).getCell(5).setCellValue(shipping.getPortOfLoading());
        sheet.getRow(13).getCell(7).setCellValue(shipping.getPortOfDischarge());


        sheet.getRow(15).getCell(0).setCellValue(packing.getMarks());
        sheet.getRow(15).getCell(2).setCellValue(packing.getDescription());

        //PackingList装箱单中有Quantity，这里等待Packing添加quantity属性
        /*sheet.getRow(15).getCell(5).setCellValue(packing.getQuantity());
        sheet.getRow(15).getCell(7).setCellValue("USD"+packing.getPackingMoney());
        double a = packing.getQuantity()*packing.getUnit();
        sheet.getRow(15).getCell(9).setCellValue(a);*/

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream); //把workbook中的内容写入到缓存流中
        new DownloadUtil().download(byteArrayOutputStream, response, "发票.xlsx");
    }
}
